package com.abbas.abchat.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author abbas
 * @description: 时间转换工具类，构造暂时只有字符串实现
 * @date 2022/4/15 23:03
 */
public class TimeUtils {

    private Date date;
    private String dateStr;
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    private static TimeUtils instance = new TimeUtils();

    //一分钟
    private long M = 1000 * 60L;
    //三分钟
    private long M3 = 1000 * 60 * 3L;
    //一个小时
    private long H = 1000 * 60 * 60L;
    //一天
    private long D = 1000 * 60 * 60 * 24L;
    //两天
    private long D2 = 1000 * 60 * 60 * 48L;
    //三天
    private long D3 = 1000 * 60 * 60 * 72L;


    /**
     * 构造 date类型
     * @param date time
     * @return this
     */
    public static TimeUtils getInstance(Date date) {
        instance.date = date;
        return instance;
    }

    /**
     * 构造 date的字符串类型，以 yyyy-MM-dd HH:mm:ss 格式解析
     * @param dateStr time
     * @return this
     */
    public static TimeUtils getInstance(String dateStr) {
        instance.dateStr = dateStr;
        return instance;
    }

    /**
     * 将构造的参数解析成calendar 与现在的时间做比较
     * 用在联系人聊天列表 显示最后一条消息距当前的时间差
     * @return timeOffset
     */
    public String compareNow() {

        Calendar calendar = Calendar.getInstance();
        try {
            calendar.setTime(sdf.parse(dateStr));
        } catch (ParseException e) {
            e.printStackTrace();
        }

        Calendar nowCalendar = Calendar.getInstance();

        final long offsetTime = nowCalendar.getTimeInMillis() - calendar.getTimeInMillis();

        if (offsetTime > D) {
            return ((int) Math.floor(offsetTime / D)) + "天前";
        } else if (offsetTime > H) {
            return ((int) Math.floor(offsetTime / H)) + "小时前";
        } else if (offsetTime > M3) {
            return ((int) (offsetTime / M)) + "分钟前";
        } else {
            return ((int) Math.floor(offsetTime / M)) + "分钟前";
        }
    }

    /**
     * 将构造的参数转化为calendar 与现在的时间作比较
     * 用于聊天列表当中 作为时间戳的转换提示
     * 两者将进行年月日的比较，从年开始，到日结束
     * 大于两年直接返回本身，大于一年返回‘前年’
     * 大于一个月返回月份本身，等于一年返回‘上个月’
     * 大于三天返回天数本身，等于两天返回‘前天’，等于一天返回‘昨天’
     * @return 比较后的转换字符串
     */
    public String compareOfChatTag() {

        StringBuilder stringBuilder = new StringBuilder("");

        Calendar calendar = Calendar.getInstance();
        try {
            calendar.setTime(sdf.parse(dateStr));
        } catch (ParseException e) {
            e.printStackTrace();
        }

        Calendar nowCalendar = Calendar.getInstance();

        int laterYear = calendar.getTime().getYear();
        int nowYear = nowCalendar.getTime().getYear();

        int laterMonth = calendar.getTime().getMonth();
        int nowMonth = nowCalendar.getTime().getMonth();

        int laterDay = calendar.getTime().getDate();
        int nowDay = nowCalendar.getTime().getDate();

        int laterHour = calendar.getTime().getHours();
//        int nowHour = calendar.getTime().getHours();

        int laterMin = calendar.getTime().getMinutes();
        int nowMin = calendar.getTime().getMinutes();

        if (nowYear - laterYear > 1) {
            return dateStr;
        } else if (nowYear - laterYear == 1) {
            return stringBuilder.append("去年").append(laterMonth).append("月").append(laterDay).append("日").toString();
        }

        if (nowMonth - laterMonth > 1) {
            return stringBuilder.append(laterMonth).append("月").append(laterDay).append("日").append(compareHour(laterHour)).append(laterMin).append("分").toString();
        } else if (nowMonth - laterMonth == 1) {
            return stringBuilder.append("上个月").append(laterDay).append("日").append(compareHour(laterHour)).append(laterMin).append("分").toString();
        }

        if (nowDay - laterDay > 2) {
            return stringBuilder.append(nowDay - laterDay).append("天前").append(compareHour(laterHour)).append(laterMin).append("分").toString();
        } else if (nowDay - laterDay == 2) {
            return stringBuilder.append("前天").append(compareHour(laterHour)).append(laterMin).append("分").toString();
        } else if (nowDay - laterDay == 1) {
            return stringBuilder.append("昨天").append(compareHour(laterHour)).append(laterMin).append("分").toString();
        } else if (laterDay - nowDay == 0) {

            stringBuilder.append(compareHour(laterHour)).append(laterMin).append("分");

        }


        return stringBuilder.toString();
    }

    private String compareHour(int laterHour) {
        StringBuilder stringBuilder = new StringBuilder("");

        //0点到六点之间
        if (laterHour >= 0 && laterHour < 6) {
            stringBuilder.append("凌晨").append(laterHour).append("点");
        } else if (laterHour >= 6 && laterHour < 12) {//上午6点到12点之间
            stringBuilder.append("上午").append(laterHour).append("点");
        } else if (laterHour == 12) {
            stringBuilder.append("中午").append(laterHour).append("点");
        } else if (laterHour > 12 && laterHour < 18) {
            stringBuilder.append("下午").append(laterHour - 12).append("点");
        } else if (laterHour >= 18 && laterHour < 23) {
            stringBuilder.append("晚上").append(laterHour - 12).append("点");
        } else if (laterHour >= 23 && laterHour <= 24) {
            stringBuilder.append("深夜").append(laterHour - 12).append("点");
        }
        return stringBuilder.toString();
    }

    //是否超过三分钟
    public boolean overThreeMin() {
        Calendar calendar = Calendar.getInstance();

        try {
            calendar.setTime(sdf.parse(dateStr));
        } catch (ParseException e) {
            e.printStackTrace();
        }

        Calendar now = Calendar.getInstance();

        long offset = now.getTimeInMillis() - calendar.getTimeInMillis();

        if (offset >= M3) return true;
        return false;
    }

    private void getOffsetMonth(int month) {

    }


}
